package com.xiaren.事务隔离级别;

import com.xiaren.service.BookingService;
import com.xiaren.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionException;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.DefaultTransactionDefinition;
import org.springframework.transaction.support.TransactionTemplate;

import javax.sql.DataSource;
import java.util.List;
import java.util.Map;

/**
 * @Description TODO
 * @Author xiaren
 * @Date 18/12/23 12:24
 **/
@Configuration
@Profile("transactional_level_profiles")    // 方便不同环境测试
public class Config1 {

    @Autowired
    BookingService bookingService;

    @Autowired
    StudentService studentService;

    @Bean
    public DataSourceTransactionManager dataSourceTransactionManager(DataSource dataSource){
        DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager(dataSource);

        return dataSourceTransactionManager;
    }

    @Bean
    public DefaultTransactionDefinition transactionDefinition(PlatformTransactionManager platformTransactionManager){
        TransactionTemplate transactionTemplate = new TransactionTemplate(platformTransactionManager);
        transactionTemplate.setIsolationLevel(TransactionDefinition.ISOLATION_READ_UNCOMMITTED);    // 未提交读

        return transactionTemplate;
    }


    // 测试幻读情况 出现情况
    @Transactional(isolation = Isolation.SERIALIZABLE)
    public boolean selectBookiing() {
        List<String> allBookings = bookingService.findAllBookings();
        System.out.println(allBookings.size());
//        try {
//            Thread.sleep(1000);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }

        List<String> allBookings1 = bookingService.findAllBookings();
        System.out.println(allBookings1.size());

        System.out.println("--------------------------");


        return allBookings.size() == allBookings1.size();


    }

    @Transactional(isolation = Isolation.READ_COMMITTED)
    public void insertBooking(){
        bookingService.book("bbb");

    }

    @Transactional(isolation = Isolation.READ_COMMITTED)
    public void selectStudent(){
        Map<String, Object> student = studentService.findByid(1L);
//        System.out.println(student);
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Map<String, Object> student2 = studentService.findByid(1L);

//        System.out.println(student2);

        System.out.println("---------------------" + student.equals(student2));
    }


    @Transactional(isolation = Isolation.SERIALIZABLE)
    public void selectStudent2(){
        List<Map<String, Object>> all = studentService.findAll();
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        List<Map<String, Object>> all2 = studentService.findAll();
        System.out.println(all.size() + "," + all2.size());
        System.out.println(all.size() == all2.size());
    }




}
